//
//  RegisterViewController.m
//  UI_元旦豆瓣半完整版
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "RegisterViewController.h"
#import "LTView.h"
#import "MyDataBaseHandle.h"
#import "RegisterModel.h"

@interface RegisterViewController ()<UITextFieldDelegate>

@end

@implementation RegisterViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"注册";
//    self.navigationController.navigationBar.translucent = NO;
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(item1Action:)];
    self.navigationItem.rightBarButtonItem = item1;
    [item1 release];
    
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"< 用户登陆" style:UIBarButtonItemStylePlain target:self action:@selector(item2Action:)];
    self.navigationItem.leftBarButtonItem = item2;
    [item2 release];
    
    LTView *name = [[LTView alloc] init];
    name.myLabel.text = @"用户名: ";
    name.myTextField.placeholder = @"请输入用户名";
    name.frame = CGRectMake(40, 100, 300, 50);
    [self.view addSubview:name];
    [name release];
    name.myTextField.delegate = self;
    name.tag = 1000;
    
    LTView *key = [[LTView alloc] init];
    key.myLabel.text = @"密码: ";
    key.myTextField.placeholder = @"请输入密码";
    key.myTextField.secureTextEntry = YES;
    key.frame = CGRectMake(40, 150, 300, 50);
    [self.view addSubview:key];
    [key release];
    key.myTextField.delegate = self;
    key.tag = 1001;
    
    LTView *sureKey = [[LTView alloc] init];
    sureKey.myLabel.text = @"确认密码: ";
    sureKey.myTextField.placeholder = @"再次输入密码";
    sureKey.frame = CGRectMake(40, 200, 300, 50);
    [self.view addSubview:sureKey];
    [sureKey release];
    sureKey.myTextField.delegate = self;
    sureKey.tag = 1002;
    
    LTView *Emil = [[LTView alloc] init];
    Emil.myLabel.text = @"邮箱: ";
    Emil.myTextField.placeholder = @"请输入邮箱";
    Emil.frame = CGRectMake(40, 250, 300, 50);
    [self.view addSubview:Emil];
    [Emil release];
    Emil.myTextField.delegate = self;
    Emil.tag = 1003;
    
    LTView *phone = [[LTView alloc] init];
    phone.myLabel.text = @"联系方式";
    phone.myTextField.placeholder = @"请输入联系方式";
    phone.frame = CGRectMake(40, 300, 300, 50);
    phone.myTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:phone];
    [phone release];
    phone.myTextField.delegate = self;
    phone.tag = 1004;
    
    
    
    
    
}
// 注册
- (void)item1Action:(UIBarButtonItem *)item {
    LTView *name = [self.view viewWithTag:1000];
    LTView *key = [self.view viewWithTag:1001];
    LTView *reKey = [self.view viewWithTag:1002];
    LTView *Emil = [self.view viewWithTag:1003];
    LTView *phone = [self.view viewWithTag:1004];
    if (name.myTextField.text.length != 0 && key.myTextField.text.length != 0 && reKey.myTextField.text.length != 0 && Emil.myTextField.text.length != 0 && phone.myTextField.text.length != 0) {
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        RegisterModel *model = [RegisterModel registerModel];
        model.name = name.myTextField.text;
        model.key = key.myTextField.text;
        model.reKey = reKey.myTextField.text;
        model.Emil = Emil.myTextField.text;
        model.phone = phone.myTextField.text;
        [[MyDataBaseHandle sharedDataBaseHandle] insertRegistModel:model];
    } else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册失败, 请重新输入" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
    
    
}

- (void)item2Action:(UIBarButtonItem *)item {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
